package utils
{
	

	/**
	 *@date 2016-1-24 下午2:27:13
	 *@author zhouhonghui
	 */
	public class HtmlUtil
	{
		/**用于匹配<(/)(ident) (attr='value')>字符串的正则表达式**/
		public static var tagBlockReg:RegExp = new RegExp("<([/]{0,1})([a-zA-Z_][a-zA-Z_0-9]*)([^>]*)>", "s");
		/**用于匹配(attr)='(value)'字符串的正则表达式**/
		public static var AttrBlockReg:RegExp = new RegExp("([a-zA-Z_][a-zA-Z_0-9]*)[ \t]*=[ |\t]*'([^']*)'|([a-zA-Z_][a-zA-Z_0-9]*)[ \t]*=[ |\t]*\"([^\"]*)\"", "sg");//标志中不能使用x因为表达式中的空格有意义
		
		
		public function HtmlUtil()
		{
		}
		
		public static function getRichTextInfo():Object
		{
			var ret:Object = {};
			
			var reg:RegExp = tagBlockReg
			
			var richText:String = "<font color='#00000000'>是都是对的<IOBJ class='Emotion' id='33'/></font>";
			
			while (richText != "")
			{
				richText = startStatement( richText );
			}
			
		/*	var ob:Object = reg.exec(str1);
			trace(str1.replace(reg,""));*/
			
			return ret;
		}
		
		protected static function startStatement(richText: String): String
		{
			//查找第一个富文本语法标签:"...(<FONT COLOR='#FFFFFF'>)ABC</FONT>..."
			var tagBlock: Object = tagBlockReg.exec(richText);
			//如果找不到则是纯文本
			if ( !tagBlock )
			{
				//addTextElement(richText);
				return "";
			}
			//如果在第一个富文本语法标签前有文本则先添加此文本:"(...)<FONT COLOR='#FFFFFF'>ABC</FONT>..."
			if ( tagBlock.index > 0 )
			{
				richText.substr(0, tagBlock.index)
				//addTextElement(richText.substr(0, tagBlock.index));
			}
			var nBlockEndIdx: int = tagBlock.index + tagBlock[0].length;//本段富文本标签结束的索引
			var identName: String = tagBlock[2].toUpperCase();
			
			//如果是结束上一个标签:"...<FONT COLOR='#FFFFFF'>ABC(</FONT>)..."
			if ( tagBlock[1] != undefined && tagBlock[1] != "" )
			{
				/*if ( identName == currentTag.tagName )
					popTag();
				else throw new Error("标签结束标记与标签起始标记不符，起始标记为：“" + currentTag.tagName + "”，结束标记为：“" + identName + "”");*/
				return richText.substring(nBlockEndIdx);
			}
			
			//对不同的富文本标记进行不同的处理
			if ( identName == "BR" )
			{
				/*flushLine();
				breakLine();*/
			}
			else 
			{
				//var tag: RichTag = pushTag(identName);
				if ( identName == "FONT" )
				{
					//fontStatement(tag, tagBlock[3]);
				}
				else if ( identName == "IMG" )
				{
					//imageStatement(tag, tagBlock[3]);
				}
				else if ( identName == "A" )
				{
					//anchorStatement(tag, tagBlock[3]);
				}
				else if ( identName == "B" || identName == "I" || identName == "U" )
				{
					//textStyleStatement(tag, identName);
				}
				else if ( identName == "P" )
				{
					//paragraphStatement(tag, tagBlock[3]);
				}
				else if ( identName == "IOBJ" )
				{
					//objectStatement(tag, tagBlock[3]);
				}
				/*if ( richText.charAt(nBlockEndIdx-2) == "/" )
					popTag();*/
			}
			return richText.substring(nBlockEndIdx);
		}
		/**
		 * 压入富文本标签 
		 * @param sTag
		 * 
		 */
		/*protected static function pushTag(sTag: String): RichTag
		{
			var tag: RichTag = new RichTag(sTag);
			//如果当前的富文本标签含有超链接且新增加了标签没有，则将当前的链接拷贝到新增加的标签中
			if ( !tag.href )
			{
				var curTag: RichTag = currentTag;
				if ( curTag && curTag.href )
				{
					tag.href = curTag.href;
					tag.id = curTag.id;
				}
			}
			m_RichTagStack.push(tag);
			return tag;
		}*/
		
		/**
		 * 创建html文本 
		 * @param content
		 * @param size	  字体大小
		 * @param color	  字体颜色(字符串或者16进制颜色)
		 * @param isLink 是否是链接文本
		 * @param evTxt	 链接文本响应事件的参数 (event:后面的字符串)
		 * @param isLine 是否下划线
		 * @param richEvTxt 富文本的链接文本响应命令串(会替换掉evTxt作为href的值)
		 * @param isBold 是否加粗
		 * @return 
		 * 
		 */		
		static public function makeHtml(content:String,color:Object = "#ffffff",size:uint=12,isLink:Boolean=false,evTxt:String="",richEvTxt:String="",isLine:Boolean=false,isBold:Boolean=false):String
		{
			if(color is int)
			{
				color = "#"+int(color).toString(16);
			}
			var ret:String = "";
			var endA:String = "";
			var endU:String = "";
			if(isLine)
			{
				endU = "</u>";
				ret += "<u>";
			}
			if(isLink)
			{
				endA = "</a>";
				var href:String=richEvTxt!=""?richEvTxt:"event:"+evTxt;
				ret += "<a href='"+href+"'>";
			}
			ret +=  "<font size='"+size+"' color='"+color+"' >"+content+"</font>";
			endA !=""?ret +=endA:"";
			endU !=""?ret +=endU:"";
			if(isBold)
			{
				ret = "<b>"+ret+"</b>";
			}
			return ret;
		}
		
		
	}
}